<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="小区" prop="areaName">
      <el-select v-model="queryParams.areaName":filter-method="filterMethod" placeholder="请输入小区" filterable allow-create
                 clearable size="small" @change="getGroup(queryParams.areaName)" default-first-option>
        <el-option v-for="(item, index) in areaIdOptions" :key="index" :label="item.areaName"
                   :value="item.areaName" :disabled="item.disabled" ></el-option>
      </el-select>
      </el-form-item>
      <el-form-item label="楼宇" prop="groupName">
        <el-select size="small" v-model="queryParams.groupName" placeholder="请输入楼宇" clearable :style="{width: '100%'}" @change="getUnit(queryParams.areaName,queryParams.groupName)">
          <el-option v-for="(item, index) in groupIdOptions" :key="index" :label="item.groupName"
            :value="item.groupName" :disabled="item.disabled" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="单元" prop="unitName">
        <el-select size="small" v-model="queryParams.unitName" placeholder="请输入单元" clearable :style="{width: '100%'}" >
          <el-option v-for="(item, index) in unitNameOptions" :key="index" :label="item.unitName"
            :value="item.unitName" :disabled="item.disabled" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="户" prop="doorName">
        <el-input
          v-model="queryParams.doorName"
          placeholder="请输入户名称"
          clearable size="small"
          :style="{width: '100%'}"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房屋编号" prop="houseId">
        <el-input
          v-model="queryParams.houseId"
          placeholder="请输入房屋编号"
          clearable size="small"
          :style="{width: '100%'}"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入姓名"
          clearable size="small"
          :style="{width: '100%'}"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search"  @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh"  @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="onPrint"
        >缴费通知书</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <div class="print-wrap" ref="print" hidden>
       <div style="position:absolute;left:0;bottom:50px;width:100%;text-align:left;">
           <div class="div2" id="skdw" style="white-space:nowrap;padding-left:30px" ></div>
               <div class="div3" id="zh" style="white-space:nowrap;padding-left:30px" ></div>
               <div class="div4" id="skrkhh" style="white-space:nowrap;padding-left:30px"  ></div>
               <div class="div5" id="userName" style="white-space:nowrap;padding-left:30px" ></div>
               <div class="div6" id="budgetMoneyDX" style="white-space:nowrap;padding-left:30px"></div>
              <div class="div7" id="budgetMoney" style="text-align:right;width:100%;letter-spacing:6px"></div>
          </div>

        </div>

    <el-table v-loading="loading" :data="doorList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="地址" align="center" prop="dooraddr" width="200"/>
      <el-table-column label="房屋编号" align="center" prop="houseId" />
      <el-table-column label="面积" align="center" prop="space" />
      <el-table-column label="实测状态" align="center" prop="spaceSts">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.HOUSE005" :value="scope.row.spaceSts"/>
        </template>
      </el-table-column>
      <el-table-column label="房屋用途" align="center" prop="purposeName" />
      <el-table-column label="姓名" align="center" prop="userName" width="120"/>
      <el-table-column label="电话" align="center" prop="tel" width="120"/>
      <el-table-column label="证件号码" align="center" prop="papersCode" width="120" show-overflow-tooltip/>
      <el-table-column  :formatter="numberFormat"  label="应缴金额" align="center" prop="budgetMoney" />
      <el-table-column  :formatter="numberFormat"  label="已缴金额" align="center" prop="costMoney" />
      <el-table-column label="最后缴费时间" align="center" prop="costDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.costDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="缴费状态" align="center" prop="costSts">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.HOUSE003" :value="scope.row.costSts"/>
        </template>
      </el-table-column>
      <el-table-column  :formatter="numberFormat"  label="本金余额" align="center" prop="balance" />
      <el-table-column label="计息方式" align="center" prop="rateType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.HOUSE004" :value="scope.row.rateType"/>
        </template>
      </el-table-column>
      <el-table-column  :formatter="numberFormat"  label="利息合计" align="center" prop="rateMoney" />
      <el-table-column label="计息时间" align="center" prop="rateDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.rateDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建者" align="center" prop="creater" />
      <el-table-column label="创建时间" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listDoor, getAreaInfo,getAreaInfo1, getGroupInfo, getUnitInfo, getPurposeInfo} from "@/api/fund/cost";
import {getNote} from "../../../api/fund/noCostQuery";
export default {
  name: "NoCostQuery",
  dicts: ['HOUSE003', 'HOUSE004','HOUSE005'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 户管理表格数据
      doorList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        areaName: null,
        groupName: null,
        unitName: null,
        doorName: null,
        houseId: null,
        purposeId: null,
        userName: null,
      },
      areaIdOptions:[{}],
      groupIdOptions:[{}],
      unitNameOptions:[{}],
      purposeOptions:[{}],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        costDate: [
          { required: true, message: "缴费时间不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.areaSelect();
    this.purposeSelect();
  },
  methods: {
    numberFormat (row, column, cellValue) {
      cellValue += ''
      if (!cellValue.includes('.')) cellValue += '.'
      return cellValue.replace(/(\d)(?=(\d{3})+\.)/g, function ($0, $1) {
        return $1 + ','
      }).replace(/\.$/, '')
    },
    onPrint(row) {
      debugger
      this.reset();
      const doorId = row.doorId || this.ids
      getNote(doorId).then(response=>{
        const printNode = this.$refs.print
        if(!printNode) return
        const newIframe = document.createElement('iframe');
        newIframe.setAttribute('style', 'width:1000px;height:1000px;left:0px;top:0px;');
        document.body.appendChild(newIframe);
        document.getElementById("userName").innerHTML=response.data.userName;
        document.getElementById("skdw").innerHTML="吉林省长白山保护开发区前郭区管理委员会物业专项维修资金专户";
        document.getElementById("zh").innerHTML="0760103011015300000143";
        document.getElementById("skrkhh").innerHTML="长白山农村商业银行前郭支行";
        document.getElementById("budgetMoney").innerHTML=response.data.budgetMoney;
        document.getElementById("budgetMoneyDX").innerHTML=response.data.budgetMoneyDX;
        let doc = null;
        doc = newIframe.contentWindow.document;
        doc.write(`
            <style type="text/css">
              @media print {
                @page {
                  size: auto;
                  margin: 1mm;
                  table { page-break-after: auto; }
                  tr    { page-break-inside: avoid; page-break-after: auto; }
                  td    { page-break-inside: avoid; page-break-after: auto; }
                  thead { display: table-header-group; }
                  tfoot { display: table-footer-group; }
                }
              }
              .print-preview { color: rgba(51, 51, 51, 1); font-family: Microsoft YaHei, '黑体', STSong, '宋体',Consolas, Monaco, Droid, Sans, Mono, Source, Code, Pro, Menlo, Lucida, Sans, Type, Writer, Ubuntu, Mono; }


            </style>
            <div style="width:100%;height:auto;width:320px;margin:0px auto;"align="center">
              ${printNode.innerHTML}
            </div>`)
        doc.close();
        setTimeout(() => {
          newIframe.contentWindow.focus();
          newIframe.contentWindow.print();
          document.body.removeChild(newIframe);
        }, 10);
      })
    },

    filterMethod(val){
      this.value=val
      getAreaInfo1(this.value).then(response => {
        this.areaIdOptions = response.rows;
      });
    },
    areaSelect(){
      getAreaInfo().then(response => {
        this.areaIdOptions = response.rows;
      });
    },
    getGroup(areaName){
      if(areaName!="") {
        getGroupInfo(areaName).then(response => {
          this.groupIdOptions = response.rows;
        });
      }
    },
    getUnit(areaName,groupName){
          getUnitInfo(areaName,groupName).then(response => {
            this.unitNameOptions = response.rows;
          });
        },
    purposeSelect(){
          getPurposeInfo().then(response => {
            this.purposeOptions = response.rows;
          });
        },
    /** 查询户管理列表 */
    getList() {
      this.loading = true;
      listDoor(this.queryParams).then(response => {
        this.doorList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.areaSelect();
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        doorId: null,
        areaName: null,
        groupName: null,
        unitName: null,
        doorName: null,
        houseId: null,
        space: null,
        spaceSts: null,
        purposeId: null,
        userName: null,
        tel: null,
        papersCode: null,
        budgetMoney: null,
        costMoney: null,
        costDate: null,
        costSts: null,
        balance: null,
        rateType: null,
        rateMoney: null,
        rateDate: null,
        creater: null,
        sysdate:null,
        unit:null,
        createDate: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.areaSelect();
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.doorId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('/fund/cost/export', {
        ...this.queryParams
      }, `未缴查询_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
